home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11277 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  88 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELPP!!! --very important-- #2
  5. Date: 22 Mar 1996 22:35:22 GMT
  6. Organization: OpenVision
  7. Message-ID: <4iv9va$95t@spanky.pls.ov.com>
  8. References: <1996Mar21.001307.138164@forest>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. Below is your program properly formatted.  Your error is to be found
  13. on the lines flagged with /****Added by FMG***/.  Basically your
  14. routine works fine when you remember to keep functions together
  15. with braces.
  16.  
  17.             Fletcher.Glenn@ov.com
  18.  
  19. #include <stdio.h>
  20. #include <math.h>
  21. #define NMAX 1000
  22.  
  23. int
  24. main(void)
  25. {
  26.  
  27.     int n = 1, min_div,         /* minimum divisor (greater than 1) of n */
  28.         step = 1;
  29.     printf("%i ", n);           /* prints out 1.. thew first prime number */
  30.     for (n = 2; n <= NMAX; n++)
  31.     {
  32.         min_div = find_div(n);
  33.         if (min_div == n)
  34.             printf("%i ", n);
  35.         step++;
  36.         if (step == 20)
  37.             printf("\n");
  38.         step = 0;               /* end of if min_div == n */
  39.         /* end for loop */
  40.     }
  41.     printf("\n");               /* nesisary carriage return (sorry about my
  42.                                  * spelling) */
  43.     /* end main */
  44. }
  45. int
  46. find_div(int n)
  47. {
  48.     int trial,                  /* current value of n */
  49.         divisor;                /* smallest divisor of n; zero means that
  50.                                  * divisor not yet found */
  51.     int even(int num);
  52.  
  53.     if (even(n))
  54.         divisor = 2;
  55.     else
  56.     { /******added by FMG *****/   
  57.         divisor = 0;
  58.         trial = 3; 
  59.     } /******added by FMG *****/
  60.  
  61.     while (divisor == 0)        /* finds is there is any possible int that
  62.                                  * would kepp it from being a prime number */
  63.         if (trial > sqrt(n))
  64.             divisor = n;
  65.         else if ((n % trial) == 0)
  66.             divisor = trial;
  67.         else
  68.             trial = trial + 2;
  69.  
  70.  
  71.     return (divisor);
  72.     /* end find_div */
  73. }
  74.  
  75. int
  76. even(int num)                   /* finds is num is even or not */
  77. {
  78.     int ans;
  79.  
  80.     ans = ((num % 2) == 0);
  81.     return (ans);               /* returns 1 if even, 0 if not */
  82.  
  83.     /* end even */
  84. }
  85.  
  86.  
  87.  
  88.